#include "GameBase.h"

GameBase::GameBase(int width, int height)
{
	_mWidth = width;
	_mHeight = height;
	_mbackBufferColor = new Color(0, 128, 255);
	FontLibrary = new FontManager;
	Window = NULL;
}
GameBase::~GameBase()
{
	if(Window != NULL)
	{
		delete Window;
		Window = NULL;
	}
	if(_mbackBufferColor != NULL)
	{
		delete _mbackBufferColor;
		_mbackBufferColor = NULL;
	}
	if(FontLibrary != NULL)
	{
		delete FontLibrary;
		FontLibrary = NULL;
	}
}

RenderWindow *GameBase::GetWindow(void)
{
	return Window;
}
FontManager *GameBase::GetFontManager()
{
	return FontLibrary;
}

bool GameBase::Start()
{
	//Try to load this
	if(!Load())
		return false;

	//Create the Window and Start the Loop
	Window = new RenderWindow(VideoMode(_mWidth, _mHeight, 32), "Squares++");
	while(Window->isOpen())
	{
		Event event;
		while(Window->pollEvent(event))
		{
			switch(event.type)
			{				
				case Event::Closed:
					Exit();

				default: break;
			}
		}

		//Clear the Back Buffer
		Window->clear(*_mbackBufferColor);

		//Do the Game Logic
		Logic();
		_fpsCounter->Update();

		//Render Everything
		OnRender();
		_fpsCounter->Render(GetWindow());
		Window->display();
	}

	return true;
}
bool GameBase::Load()
{
	//// **********************
	////Load the Fonts
	Font *Arial18 = new Font;
	if(!Arial18->loadFromFile("C:/Windows/Fonts/Arial.ttf"))
	{
		//MessageBox(NULL, L"Error loading Arial.tff", NULL, NULL);
		return false;
	}	
	GetFontManager()->AddFont("Arial18", Arial18); 
	
	// **********************
	//Load the FPS Counter
	_fpsCounter = new FPSCounter();
	if(!_fpsCounter->Load(GetFontManager()))
		return false;

	return OnLoad();
}
void GameBase::Exit()
{
	Window->close();
}